package com.vates.gestion.dao.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.vates.gestion.dao.BaseDao;
import com.vates.gestion.entidades.TipoHabitacion;

@Repository(value = "tipoHabDao")
public class TipoHabDaoImpl implements BaseDao<TipoHabitacion> {

	private EntityManager em;

	/*
	 * Sets the entity manager
	 */

	@PersistenceContext
	public void setEntityManager(EntityManager em) {
		this.em = em;

	}

	@Override
	@Transactional(readOnly = false)
	public TipoHabitacion nuevo(TipoHabitacion entidad) {

		return em.merge(entidad);
	}

	@Override
	public TipoHabitacion buscarPorId(Long id) {

		return em.find(TipoHabitacion.class, id);

	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public List<TipoHabitacion> buscarTodos() {

		return em.createQuery("select t from TipoHabitacion t order by t.id")
				.getResultList();

	}

	@SuppressWarnings("unchecked")
	@Override
	@Transactional(readOnly = true)
	public List<TipoHabitacion> buscarPorNombre(String nombre) {

		return em
				.createQuery(
						"select t from TipoHabitacion t where t.nombre LIKE :nombre order by t.id")
				.setParameter("nombre", nombre).getResultList();

	}

	@Override
	public void borrar(TipoHabitacion entidad) {

		em.remove(entidad);

	}

}
